<template>
  <div class="salesanalysis-detail">
    <Row type='flex'
         justify="space-between"
         class="coinrevenue-list-edit">
      <Row type='flex'
           align='middle'>
        <Col>
        <span class="concrete-data">时间：</span>
        <datePickerYmd @on-change="search"
                       :selectDateType.sync='selectDateType'
                       :dataType='dataType'
                       :dateOption="dateOption"
                       v-model="timeDate"
                       style="display:inline-block"></datePickerYmd>
        </Col>
        <Col>
        <span class="concrete-data">门店名称：</span>
          <Input v-model="shopName" style="width:180px;" disabled/>
        </Col>
        <Row type='flex'
             align='middle'
             class="export-btn">
          <Col><span>手机号：</span></Col>
          <Col>
          <Input v-model="mobileNumber"
                 v-filterinput="{reg: regExp.nan, type: 'integerNumber'}"
                 :maxlength="11"
                 autocomplete="off"
                 style="width:180px;"
                 placeholder="请输入" />
          </Col>
          <Col>
          <Button type="primary"
                  class="coinrevenue-btn export-btn"
                  style="width:80px;"
                  @click="search">查询</Button>
          </Col>
        </Row>
      </Row>
      <Col class="export_receivable"
           v-if="hasPermission('moms:report:membersvipdetailanalyticexport')">
      <Button class="check_all"
              @click="DropdownItemn"
              style="width:80px">导出
      </Button>
      </Col>
    </Row>
    <Table ref="rowSpanTable"
           stripe
           border
           :height="tableHeight"
           :loading='loading'
           class="table-list"
           :columns="columns"
           :data="resData">
      <template slot-scope="{ row }"
                slot="mobileNumber">
        <span>{{telClear(row.mobileNumber)}}</span>
      </template>
       <template slot-scope="{ row }"
                slot="purchaseAmount">
        <span :style="{color:(row.purchaseAmount.indexOf('-')>-1?'red':'')}">{{row.purchaseAmount}}</span>
      </template>
    </Table>
    <div class="page-box"
         style="margin-top: 20px">
      <Page :total="total"
            ref="pages"
            :current.sync="searchObj.offset"
            :page-size="searchObj.limit"
            @on-change="handlePage"
            @on-page-size-change="handlePageSize"
            show-sizer
            :page-size-opts="$pageSizeOpts"
            show-total />
    </div>
      <down-center ref="downCenterRef"
            perms="moms:report:membersvipdetailanalyticexport"
            v-if="hasPermission('moms:report:membersvipdetailanalyticexport')"></down-center>
  </div>
</template>
<script>
import vueTreeselectChoice from '@/components/vue-treeselect-choice'
import datePickerYmd from '@/components/date-picker-ymd/date-picker-ymd.vue'
import dayjs from 'dayjs'
import regExp from '@/libs/regExp'
import { svipSaleDetail } from '@/api/report/svip'
import DownCenter from '@/view/components/down-center/index.vue'
import { mapState } from 'vuex'
import { telClear } from '@/libs/tools'
export default {
  name: 'report_member_salesanalysis_detail',
  components: { vueTreeselectChoice, datePickerYmd, DownCenter },
  data () {
    return {
      dataType: [{ key: 'date', value: '日' }, { key: 'month', value: '月' }, { key: 'year', value: '年' }],
      // selectDateType: 'date',
      // timeDate: [dayjs().subtract(1, 'day').format('YYYY-MM-DD 00:00:00'), dayjs(new Date()).format('YYYY-MM-DD 00:00:00')],
      timeDate: this.$route.query.timeDate,
      selectDateType: this.$route.query.selectDateTyp,
      dateOption: {
        disabledDate: date => {
          if (process.env.NODE_BUILD === 'prod') return dayjs(date).isAfter(dayjs(new Date()).subtract(1, 'day'))
          return dayjs(date).isAfter(dayjs(new Date()))
        }
      },
      ShopValue: [],
      shopName: this.$route.query.shopName,
      total: 0,
      regExp: regExp,
      telClear: telClear,
      searchObj: {
        shopId: [],
        offset: 1,
        limit: 10
      },
      loading: false,
      resData: [],
      mobileNumber: '',
      tableHeight: 0,
      columns: [
        { title: '购买时间', key: 'purchaseTime', align: 'center' },
        { title: '手机号', slot: 'mobileNumber', align: 'center' },
        { title: 'SVIP购买金额', slot: 'purchaseAmount', align: 'center' }
      ]
    }
  },
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId,
      userId: state => state.user.userId,
      userName: state => state.user.userName
    }),
    startDate () {
      return this.timeDate[0]
    },
    endDate () {
      return this.timeDate[1]
    }
  },
  destroyed () {
    // localStorage.removeItem('svipAn')
  },
  mounted () {
    this.timeDate = this.$route.query.timeDate
    // console.log(this.$route.query.timeDate.selectDateType)
    this.selectDateType = this.$route.query.selectDateType
    this.search()
  },
  methods: {
    search () {
      this.searchObj.offset = 1
      this.getList()
      // if (!dayjs(this.startDate).isValid()) {
      //   this.$Message.error(`请选择时间`)
      // } else if (!this.searchObj.shopId.length) {
      //   this.$Message.error(`请选择门店列表`)
      // } else {
      //   this.searchObj.offset = 1
      //   this.searchObj.limit = 10
      //   this.getList()
      // }
    },
    DropdownItemn () {
      // if (!this.searchObj.shopId.length) return this.$Message.error(`请选择门店列表`)
      this.$refs.downCenterRef.addTask({
        merchantId: this.$store.state.user.merchantId,
        shopIds: this.$route.query.id,
        startDate: this.startDate,
        endDate: this.endDate,
        dateType: { year: 2, month: 1, date: 0 }[this.selectDateType],
        taskModule: 'svipSalesDetail',
        extraMapParam: {
          mobileNumber: this.mobileNumber
        }
      })
    },
    getList () {
      this.loading = true
      this.tableHeight = 0
      this.resData = []
      svipSaleDetail({
        mobileNumber: this.mobileNumber,
        merchantId: this.$store.state.user.merchantId,
        shopIds: this.$route.query.id,
        startDate: this.startDate,
        endDate: this.endDate,
        offset: (this.searchObj.offset - 1) * this.searchObj.limit,
        limit: this.searchObj.limit
      }).then(res => {
        this.loading = false
        if (res.data.status === 200) {
          this.total = res.data.totalCount
          if (res.data.data && res.data.data.length >= 14) {
            this.tableHeight = 520
          }
          this.resData = res.data.data ? res.data.data : []
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    handlePage (val) {
      this.searchObj.offset = val
      this.getList()
    },
    handlePageSize (pageSize) {
      this.searchObj.limit = pageSize
      this.getList()
    }
  }
}
</script>
<style lang="less" scoped>
.coinrevenue-list-edit {
  padding: 20px;
  // min-width: 1200px;

  .concrete-data {
    margin-left: 10px;
  }

  .common-input {
    width: 180px;
  }

  .vue-treeselect__control {
    height: 32px;
  }

  .coinrevenue-btn {
    width: 80px;
  }

  .coinrevenue-btn2 {
    width: 100px;
  }

  .export-btn {
    margin-left: 24px;
  }

  .marginTop {
    padding-top: 20px;
  }
}
</style>
